function doLogin(app, options) {
    wx.login({
        success: function(res) {
            if (res.code) {
                app.getUserInfo(function(userInfo){
                    var nickName = userInfo.nickName;
                    var avatarUrl = userInfo.avatarUrl;
                    //根据code换取user的openid
                    var userService = require('../services/userService');

                    userService.getOpenIdByCode({
                      authCode: res.code,
                      success: function (res) {
                        if (res.openid) {
                          options.success.call(this, {
                            openid: res.openid,
                            avatar: avatarUrl,
                            nickname: nickName
                          });
                        }
                      },
                      fail: function () {
                        wx.showToast({
                          title: '获取用户身份失败',
                          icon: 'loading',
                          duration: 2000
                        });
                      }
                    });
                });
            
            } else {
                wx.showToast({
                    title: res.errMsg,
                    icon: 'loading',
                    duration: 2000
                });
            }
        }
    });
}

module.exports = {
  doLogin: doLogin,
}